package com.zhuss.core.permission;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.permission.CarePermissionResourceListBo;
import com.zhuss.bean.bo.core.permission.CarePermissionRoleListBo;
import com.zhuss.bean.bo.core.permission.DetailPermissionBo;
import com.zhuss.bean.bo.core.permission.ListPermissionTreeBo;
import com.zhuss.bean.bo.core.permission.SearchPermissionBo;
import com.zhuss.bean.vo.core.permission.CarePermissionResourceListVo;
import com.zhuss.bean.vo.core.permission.CarePermissionRoleListVo;
import com.zhuss.bean.vo.core.permission.DetailPermissionVo;
import com.zhuss.bean.vo.core.resource.ListResourceTreeVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.ReadService;
import com.zhuss.core.processor.PermissionRead;
import com.zhuss.core.processor.PermissionResourceRead;
import com.zhuss.core.processor.ResourceRead;
import com.zhuss.core.processor.RolePermissionRead;
import com.zhuss.core.processor.RoleRead;
import com.zhuss.enums.选中状态;
import com.zhuss.exception.AabException;


/**
 * 权限点查询service（基本查询）
 * @author zhushanshuo
 * @Date 2019-12-13 15:59
 *
 */
@Component("permissionReadService")
public class PermissionReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("permissionRead")
	private PermissionRead permissionRead;
	
	@Autowired
	@Qualifier("resourceRead")
	private ResourceRead resourceRead;
	
	@Autowired
	@Qualifier("roleRead")
	private RoleRead roleRead;
	
	@Autowired
	@Qualifier("permissionResourceRead")
	private PermissionResourceRead permissionResourceRead;
	
	@Autowired
	@Qualifier("rolePermissionRead")
	private RolePermissionRead rolePermissionRead;
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws AabException {
		DetailPermissionBo bo = (DetailPermissionBo) param;
		Long permissionId = bo.getPermissionId();
		Object vo = permissionRead.detail(permissionId);
		return vo;
	}
	
	/**
	 * 列表
	 * @throws AabException 
	 */
	@Override
	public Object list(Object param) throws AabException {
		SearchPermissionBo bo = (SearchPermissionBo) param;
		Object result = permissionRead.list(bo);
		return result;
	}
	
	/**
	 * 权限树
	 * @param bo
	 * @return
	 */
	public Object listPermissionTree(ListPermissionTreeBo bo) {
		
		Integer system = bo.getSystem();
		return permissionRead.listPermissionTree(system);
	}
	
	/**
	 * 权限的资源树列表
	 * @param bo
	 * @return
	 * @throws AabException 
	 */
	@SuppressWarnings("unchecked")
	public Object carePermissionResourceList(CarePermissionResourceListBo bo) throws AabException {
		
		Long permissionId = bo.getPermissionId();
		Integer system = ((DetailPermissionVo) permissionRead.detail(permissionId)).getSystem();
		CarePermissionResourceListVo result = new CarePermissionResourceListVo();
		result.setResourceIdList((List<Long>)permissionResourceRead.listResourceId(permissionId, 选中状态.全选中.key()));
		result.setResourceTreeList((List<ListResourceTreeVo>)resourceRead.listResourceTreeNoPublic(Arrays.asList(system)));
		return result;
	}
	
	/**
	 * 权限的角色列表
	 * @param bo
	 * @return
	 * @throws AabException 
	 */
	@SuppressWarnings("unchecked")
	public Object carePermissionRoleList(CarePermissionRoleListBo bo) throws AabException {
		
		Long permissionId = bo.getPermissionId();
		
		CarePermissionRoleListVo result = new CarePermissionRoleListVo();
		result.setRoleIdList((List<Long>)rolePermissionRead.listRoleId(permissionId));
		result.setRoleList((List<ListVo>)roleRead.listRole());
		return result;
	}
	
	
	
}

